package mvc.importer.impl;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import mvc.core.impl.MachuImage;
import mvc.core.impl.MachuPicchu;
import mvc.imageProcessor.IImageProcessor;
import mvc.importer.IImporter;
import mvc.utils.FileUtilitys;

import com.google.inject.Inject;

public class ImportHandler {
	private final IImporter importer;
	private final IImageProcessor imageProcessor;

	@Inject
	public ImportHandler(IImporter importer, IImageProcessor imageProcessor) {
		this.importer = importer;
		this.imageProcessor = imageProcessor;
	}

	public List<MachuImage> getImages() throws IOException {
		List<MachuImage> imageList = new LinkedList<MachuImage>();
		File path = new File(MachuPicchu.getInstance().getUniqueRuntimePath());
		List<File> files = importer.getFiles();
		if (path.exists() == false && files.size() > 0) {
			System.out.println("Created directory: " + path.getAbsolutePath());
			path.mkdir();
			path.deleteOnExit();
		}
		for (File f : files) {
			File newFile = new File(path.getAbsolutePath() + File.separator
					+ f.getName());
			int i = 0;
			while (newFile.exists()) {
				newFile = new File(path.getAbsolutePath() + File.separator + i
						+ f.getName());
				i++;
			}
			FileUtilitys.copyFile(f, newFile);
			System.out.println("Created file: " + newFile.getAbsolutePath());
			imageList.add(new MachuImage(newFile));
		}
		return imageList;
	}
}